<template>
  <div>
    <div class="login-box">
      <div class="form-box">
        <el-card class="box-card">
          <div slot="header" class="clearfix header">
            <span>黑马面经运营后台</span>
          </div>
          <el-form :model="form" :rules="rules" ref="form">
            <el-form-item label="用户名" prop="username">
              <el-input
                placeholder="请输入用户名"
                v-model="form.username"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="密码"
              prop="password"
              @keyup.enter.native="login"
            >
              <el-input
                placeholder="请输入用户密码"
                v-model="form.password"
                type="password"
              ></el-input>
            </el-form-item>
            <el-form-item class="buts">
              <el-button type="primary" @click="login" :loading="flag"
                >登录</el-button
              >
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'index-mage',
  components: {},
  props: {},
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
          // {
          //   pattern: /^[a-z]{5,11}$/,
          //   message: '密码必须是字母并且长度是5到11位',
          //   trigger: 'blur'
          // }
        ]
      },
      flag: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    async login() {
      try {
        await this.$refs.form.validate()
        this.flag = true
        console.log('发送ajax')
        const res = await login(this.form)
        console.log(res)
        this.$store.commit('user/setToken', res.data.token)
        this.flag = false
        this.$message.success('登录成功')
        this.$router.push('/layout')
      } catch (err) {
        console.log(err)
        if (err.response && err.response.status === 400) {
          this.$message.error('用户名或密码不存在')
        } else {
          this.$message.error('出错了')
        }
        this.flag = false
        // this.$message.error('表单验证失败')
      }
    },
    reset() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('@/styles/login-bg.svg') no-repeat center center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .form-box {
    width: 420px;
    height: 368px;
    // background-color: pink;
  }
  .header {
    text-align: center;
    color: #fff;
  }
}
::v-deep .el-card__header {
  background-color: #727cf5;
}
.buts {
  text-align: center;
  .el-button:first-child {
    background-color: #727cf5;
  }
}
</style>
